package com.jstyle.jclife.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.jstyle.jclife.R;
import com.jstyle.jclife.utils.ScreenUtils;

public class BPLevelView extends View {
    private  int defaultHeight;
    private  int defaultWidth;
    Bitmap bitmapLevel;
    Bitmap bitmapTop;
    Paint mPaint;
    int paddingTop;
    int level=0;
    String[]levvelString=new String[]{"Low","Normal","Mild","Moderate","High"};
    private Matrix matrix;
    float ratio;
    public BPLevelView(Context context,  @Nullable AttributeSet attrs) {
        super(context, attrs);
        bitmapLevel= BitmapFactory.decodeResource(context.getResources(), R.drawable.bp_history_level);
        bitmapTop= BitmapFactory.decodeResource(context.getResources(), R.drawable.bp_view_top);

        init(context);
    }

    private void init(Context context) {
        mPaint=new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setTextSize(ScreenUtils.dip2px(context, 15));
        int screenWidth=ScreenUtils.getScreenWidth(context);
        float ratioWidth=screenWidth*0.75f;
        int bitMapWidth=bitmapLevel.getWidth();
        ratio=bitMapWidth/ratioWidth;

        defaultHeight = ScreenUtils.dip2px(context, 50);
        defaultWidth = ScreenUtils.dip2px(context, 350);

        paddingTop=ScreenUtils.dip2px(context, 4);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int top = (getHeight() - bitmapLevel.getHeight()) / 2;
        int left = (getWidth() - bitmapLevel.getWidth()) / 2;
        matrix = new Matrix();
        matrix.setTranslate(left,top);
        matrix.postScale(ratio, ratio, getWidth()/2, getHeight()/2);
        canvas.drawBitmap(bitmapLevel, matrix, null);
        int realWidth= (int) (bitmapLevel.getWidth()*ratio);
        int startX = getWidth() / 2 - realWidth / 2;
        int width = realWidth/ 5;
        int bitMapx = level * width - bitmapTop.getWidth() / 2 - width / 2;
        if(level!=0)
            canvas.drawBitmap(bitmapTop, startX + bitMapx, top - bitmapTop.getHeight(), mPaint);
        drawText(canvas, top,startX,width);

    }

    private void drawText(Canvas canvas,int top,int startX,int width) {
        int size=levvelString.length;
        Rect rect=new Rect();

        for(int i=0;i<size;i++){
            String text=levvelString[i];
            mPaint.getTextBounds(text,0,text.length(),rect);
            int textLength=rect.width();
            int textHeight=rect.height();
            int x=(i+1)*width-textLength/2-width/2;
            int y=top+bitmapLevel.getHeight();
            canvas.drawText(text,startX+x,y+textHeight+paddingTop,mPaint);
        }
    }

    public void setBpLevel(int level){
        this.level=level;
        postInvalidate();
    }


    private static final String TAG = "BPLevelView";

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST||MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {
            height = defaultHeight;
        }
        if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST||MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED) {
            width = defaultWidth;
        }
        setMeasuredDimension(width, height);
    }
}
